/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "DBDayFile.h"
#include "StockDatabase.h"
#include <sstream>

namespace QSThink
{
	const std::string DBDayIndexFile::IndexFileName="Day.index";


	DBDayIndexFile::DBDayIndexFile()
	{
		_dayCount=0;
		memset(_days,0,DAY_MAX*sizeof(int));

		FILE* file=fopen(IndexFileName.c_str(),"rb");//wb+ write file
		if(!file){
			return;
		}

		fseek(file,0L,SEEK_SET);

		fread(&_dayCount,sizeof(_dayCount),1,file);
		fread(_days,sizeof(int),_dayCount,file);

		fclose(file);

		//add symbol to map for quick search

		for(int i=0;i<_dayCount;i++){
			_dayMap.insert(std::map<int,int>::value_type(_days[i],i));
		}

	}

	int DBDayIndexFile::addDay( int d )
	{
		std::map<int,int>::iterator it=_dayMap.find(d);
		if(it!=_dayMap.end()){
			//find
			return it->second;
		}

		_days[_dayCount]=d;
		_dayMap.insert(std::map<int,int>::value_type(d,_dayCount));
		_dayCount++;

		FILE* file=fopen(IndexFileName.c_str(),"wb");
		fseek(file,0L,SEEK_SET);

		//write day count
		fwrite(&_dayCount,sizeof(_dayCount),1,file);
		fwrite(_days,sizeof(int),_dayCount,file);

		fclose(file);

		return _dayCount-1;


	}

	int DBDayIndexFile::getDayCount()
	{

		return _dayCount;
	}

	bool DBDayIndexFile::hasDay( int day )
	{
		std::map<int,int>::iterator it=_dayMap.find(day);
		if(it!=_dayMap.end()){
			//find
			return true;
		}
		return false;

	}

	int DBDayIndexFile::getDayIndex( int day )
	{

		std::map<int,int>::iterator it=_dayMap.find(day);
		if(it!=_dayMap.end()){
			//find
			return it->second;
		}
		return -1;

	}

	StockDayInfo::StockDayInfo(): flag(0),symbol(0),openPrice(0),
		closePrice(0),highPrice(0),lowPrice(0),volume(0),amount(0)
	{
		for(int i=0;i<9;i++){
			unused[i]=0;
		}
	}

	void StockDayInfo::set( int s,int o,int c, int h,int l,int v,int a )
	{
		flag=1;
		symbol=s;
		openPrice=o;
		closePrice=c;
		highPrice=h;
		lowPrice=l;
		volume=v;
		amount=a;

	}



	DBDayFile::DBDayFile(int day) : _day(day)
	{
		std::stringstream ss;
		ss << day;

		_fileName=ss.str();
		_fileName+=".day";

		FILE* file=fopen(_fileName.c_str(),"rb");//wb+ write file
		if(!file){
			return;
		}

		fseek(file,0L,SEEK_SET);

		fread(_stock,sizeof(StockDayInfo),StockMax,file);


		fclose(file);

	}

	void DBDayFile::persit(int index,StockInfo info )
	{

		int ret=StockDatabase::getSingleton().getSymbolIndex(info.symbol);
		assert(ret==index&&index!=-1);

		_stock[index].set(info.symbol,info.openPrice*100,info.closePrice*100,
			info.highPrice*100,info.lowPrice*100,info.volume,info.amount);


		FILE* file=fopen(_fileName.c_str(),"wb");
		fseek(file,0L,SEEK_SET);

		//not write stock count for we always write StockMax(1024)
		fwrite(_stock,sizeof(StockDayInfo),StockMax,file);

		fclose(file);

		//
	}

	StockDayInfo* DBDayFile::getStockInfo( int symbol )
	{
		int index=StockDatabase::getSingleton().getSymbolIndex(symbol);
		if(index>=0){
			if(_stock[index].flag==1){
				return _stock+index;
			}
			return 0;
		}
		return 0;
	}

}
